//
//  CategoriesProductListVC.m
//  AFNetworking
//
//  Created by admin on 2020/4/29.
//

#import "CategoriesProductListVC.h"
#import "PageHeaderCollectionView.h"
#import "ProudctCollectionCell.h"
@implementation CategoriesProductListToolView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self confitUI];
    }
    return self;
}

-(void)confitUI{
    self.backgroundColor = UIColor.whiteColor;
    [self ug_shadowColor:UIColor.blackColor width:1];
    [self addSubview:self.creatBtn];
    [self addSubview:self.sorftBtn];
}
-(UIButton *)sorftBtn{
    if (!_sorftBtn) {
        _sorftBtn = [UIButton new];
        [_sorftBtn ug_radius:4];
        _sorftBtn.titleLabel.font = FONT_SYS14;
        [_sorftBtn setTitleColor:[UIColor ug_hexString:@"#007AFF"] forState:UIControlStateNormal];
        [_sorftBtn ug_borderColor:[UIColor ug_hexString:@"#007AFF"] width:1];
        [_sorftBtn setTitle:@"商品排序" forState:UIControlStateNormal];
    }
    return _sorftBtn;
}

-(UIButton *)creatBtn{
    if (!_creatBtn) {
        _creatBtn = [UIButton new];
        [_creatBtn setTitle:@"添加新商品" forState:UIControlStateNormal];
        [_creatBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_creatBtn ug_radius:4];
        _creatBtn.titleLabel.font = FONT_SYS14;
        [_creatBtn setBackgroundColor:[UIColor ug_hexString:@"#007AFF"]];
        
    }
    return _creatBtn;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [_sorftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.width.mas_equalTo(KAutoAcale(80));
        make.height.mas_equalTo(KAutoAcale(32));
        make.right.mas_equalTo(self.creatBtn.mas_left).mas_offset(-KPAND_DEF);
    }];
    [_creatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.width.mas_equalTo(KAutoAcale(80));
        make.height.mas_equalTo(KAutoAcale(32));
        make.right.mas_equalTo(self).mas_offset(-KPAND_DEF);
    }];
}

@end

@interface CategoriesProductListVC ()<UITableViewDelegate,UITableViewDataSource>

@property(strong, nonatomic) UITableView *tableView;

@property(strong, nonatomic) PageHeaderCollectionView *headView;
@property(strong, nonatomic) CategoriesProductListToolView *fotterView;

@property (nonatomic, strong) NSMutableArray * datalist;

@end

@implementation CategoriesProductListVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self configUI];
    [self reloadData];
}

-(void)configUI{
    
    self.view.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:self.headView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.fotterView];
}


-(NSMutableArray *)datalist{
    if (!_datalist) {
        _datalist = [@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"] mutableCopy];
    }
    return _datalist;
}
-(PageHeaderCollectionView *)headView{
    if (!_headView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        
        layout.minimumLineSpacing = 0.0;
        layout.minimumInteritemSpacing = 0.0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(KWidth/2, 44);
        _headView = [[PageHeaderCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        NSArray *items = [NSArray modelArrayWithClass:[PageHeaderCollectionCellData class] json:@[
            @{@"title":@"出售中" },
            @{@"title":@"已下架" }
        ]];
        [_headView setItems:items];
        UG_WEAKSELF
        [_headView setCellClick:^(id  _Nonnull item) {
            [weakSelf reloadData];
        }];
        _headView.backgroundColor = UIColor.whiteColor;
    }
    return _headView;
}
-(CategoriesProductListToolView *)fotterView{
    if (!_fotterView) {
        _fotterView = [CategoriesProductListToolView new];
        UG_WEAKSELF
        [_fotterView.creatBtn ug_addEvents:UIControlEventTouchUpInside andBlock:^(id  _Nonnull sender) {
            [weakSelf routepush:@"ProductEditVC" param:@{}];
        }];
        [_fotterView.sorftBtn ug_addEvents:UIControlEventTouchUpInside andBlock:^(id  _Nonnull sender) {
            [weakSelf routepush:@"ProductSoftVC" param:@{@"title":weakSelf.title}];
        }];
        
    }
    return _fotterView;
}

-(void)reloadData{
    UG_WEAKSELF
    [self.view ug_starloading];
    [self bk_performBlock:^(id obj) {
        PageHeaderCollectionCellData *data = weakSelf.headView.selectItem;
        if ([data.title  isEqualToString:@"出售中"]) {
            weakSelf.datalist = [@[@"0-1",@"0-1",@"0-2",@"0-3",@"0-4",@"0-5",@"0-6",@"0-7",@"0-8",@"0-9"] mutableCopy];
        }else{
            weakSelf.datalist = [@[@"1-1",@"1-1",@"1-2"] mutableCopy];
        }
        [self.view ug_stoploading];
        [weakSelf.tableView reloadData];
    } afterDelay:0.35];
}
-(UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorColor:COLOR_EE];
        [_tableView setTableFooterView:[UIView new]];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datalist.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* indentifier = @"cell";
    ProudctCollectionCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[ProudctCollectionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return KAutoAcale(102);
}


-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.headView.mas_bottom);
        make.bottom.mas_equalTo(self.fotterView.mas_top);
    }];
    [_fotterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(KSAFE_Bottom+44);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
}


@end
